package chat.view.dialogs;

import chat.clients.XmppClient;
import chat.commands.Login;
import org.jivesoftware.smack.XMPPException;
import swing.FormPanel;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.util.prefs.Preferences;

import static java.util.prefs.Preferences.userNodeForPackage;

public class LoginDialog extends swing.Dialog {

    /**
     *
     */
    private static final long serialVersionUID = 4511285615251920353L;

    private static final int TOP_MARGIN = 10;
    private static final int LEFT_MARGIN = 10;
    private static final int BOTTOM_MARGIN = 10;
    private static final int RIGHT_MARGIN = 10;

    private final Preferences preferences;

    private final FieldsPane fieldsPane;
    private final XmppClient client;

    public LoginDialog(final XmppClient client) {
        this.client = client;
        preferences = userNodeForPackage(client.getClass());

        JPanel contentPane = new JPanel();
        setContentPane(contentPane);
        contentPane.setBorder(new EmptyBorder(TOP_MARGIN, LEFT_MARGIN, BOTTOM_MARGIN, RIGHT_MARGIN));
        setTitle("Login");

        fieldsPane = new FieldsPane();
        getContentPane().add(fieldsPane);

        pack();
        setLocationRelativeTo(null);
    }

    private class FieldsPane extends FormPanel {
        /**
         *
         */
        private static final long serialVersionUID = -2541524500175893220L;


        private final JTextField usernameField = new JTextField(20);
        private final JPasswordField passwordField = new JPasswordField(20);
        private final JCheckBox rememberMeField = new JCheckBox();

        private FieldsPane() {
            super("Login");

            addField("Username", usernameField);
            addField("Password", passwordField);
            addField("Remember me", rememberMeField);

            String username = preferences.get("username", null);
            if (username != null)
                usernameField.setText(username);

            String password = preferences.get("password", null);
            if (password != null)
                passwordField.setText(password);

            defaultButton.addActionListener(e -> login());
        }
    }

    private void login() {
        setComponentEnabled(false);
        Login login = new Login(client);

        final String username = fieldsPane.usernameField.getText();
        login.setUsername(username);

        final char[] password = fieldsPane.passwordField.getPassword();
        login.setPassword(password);

        login.onFinished((o1, arg1) -> {
            JOptionPane.showMessageDialog(LoginDialog.this, "Ok");
            LoginDialog.this.setVisible(false);
            if (fieldsPane.rememberMeField.isSelected()) {
                preferences.put("username", username);
                preferences.put("password", new String(password));
            }
        });
        login.onAborted((o, arg) -> {
            popup((XMPPException) arg);
            setComponentEnabled(true);
            fieldsPane.usernameField.setText("");
            fieldsPane.passwordField.setText("");
        });
        login.execute();

    }
}
